/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.teacake.filteringproxy;
import java.util.HashMap;


/**
 *
 * @author tkleisas
 */

public class HttpHeader {
    public HttpHeader(byte[] data) throws RuntimeException
    {
        m_header = new HashMap<String,String>();
        int index = Utility.find(data, "\r\n\r\n".getBytes());
        if (index<0)
        {
            throw new RuntimeException("Parsing error");
        }
        byte[] headerData = Utility.copyFromBuffer(data, 0, index+2 );
        if(headerData==null)
        {
            throw new RuntimeException("Parsing error");
        }
        if(headerData.length==0)
        {
            throw new RuntimeException("Parsing error");
        }
        index = Utility.find(data, "\r\n".getBytes());
        if(index <0)
        {
            throw new RuntimeException("Parsing error");
        }
        byte[] headerLine = Utility.copyFromBuffer(headerData, 0, index);

        headerData = Utility.copyFromBuffer(headerData, index+2, headerData.length-index-2);
        String str = new String(headerData);
        String headerstring = new String(headerLine);
        m_firstline = headerstring;
        
        while(headerData.length>0)
        {
            index = Utility.find(headerData, "\r\n".getBytes());
            if(index<0)
            {
                throw new RuntimeException("Parsing error");
            }
            headerLine = Utility.copyFromBuffer(headerData, 0, index );
            
            headerData = Utility.copyFromBuffer(headerData, index+2, headerData.length-index-2);
            str = new String(headerData);
            if(headerLine.length==0)
            {
                throw new RuntimeException("Parsing error");
            }
            String headerStringLine = new String(headerLine);
            System.out.println("["+headerStringLine+"]\r\n");
            String key = headerStringLine.substring(0,headerStringLine.indexOf(":")+1);
            String value = headerStringLine.substring(headerStringLine.indexOf(":")+1,headerStringLine.length());
            m_header.put(key, value);
        }
    }
    public String getFirstLine()
    {
        return this.m_firstline;
    }
    public String getValue(String key)
    {
        return m_header.get(key);
    }
    public String printHeaders()
    {
        StringBuffer strbuf = new StringBuffer();
        strbuf.append("version:");
        strbuf.append(m_version);
        strbuf.append("\r\n");
        strbuf.append("response code:");
        strbuf.append(m_response_code);
        strbuf.append("\r\n");
        strbuf.append("response result:");
        strbuf.append(m_response_result);
        strbuf.append("\r\n");
        strbuf.append(m_firstline);
        strbuf.append("\r\n");
        java.util.Iterator<String> iter = m_header.keySet().iterator();

        while(iter.hasNext())
        {
            String key = iter.next();
            String value = m_header.get(key);
            strbuf.append(key);
            strbuf.append(value);
            strbuf.append("\r\n");
        }
        return strbuf.toString();
    }
    private HashMap<String,String> m_header;
    private String m_firstline;
    private String m_version;
    private String m_response_code;
    private String m_response_result;
}
